package ys.mihoyo.ymcc.service.impl;

import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import ys.mihoyo.ymcc.result.JSONResult;
import ys.mihoyo.ymcc.domain.Login;
import ys.mihoyo.ymcc.mapper.LoginMapper;
import ys.mihoyo.ymcc.service.ILoginService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 登录表 服务实现类
 * </p>
 *
 * @author mihoyo
 * @since 2022-12-04
 */
@Service
public class LoginServiceImpl extends ServiceImpl<LoginMapper, Login> implements ILoginService {
    @Autowired
    private LoginMapper loginMapper;

    @Override
    @GlobalTransactional(rollbackFor = Exception.class)//全局事务
    public JSONResult register(Login login) {
        //判断手机号是否已经注册
        Login login1 = loginMapper.selectByUsername(login.getUsername());
        if (login1 != null) {
            return JSONResult.error("手机号已经注册");
        }
        Integer insert = loginMapper.insert(login);
        //判断是否插入成功
        if (insert == 1) {
            return JSONResult.success(login.getId());
        } else {
            return JSONResult.error("注册失败");
        }
    }
}
